<?php
namespace Swiftx\Bundle\WeChat\View;
use Dwoo\Core;
use Dwoo\Data;
use Swiftx\Bundle\WeChat\System\Config;
use Swiftx\Bundle\WeChat\System\Storage;
use Swiftx\Interfaces\Router\Collection;

/**
 * 用户登录页面实现
 */
abstract class Common {

    /**
     * 配置对象
     * @var Config
     */
    protected $config;

    /**
     * 模板引擎
     * @var Core
     */
    protected $engine;

    /**
     * 视图数据
     * @var Data
     */
    protected $data;

    /**
     * 模板文件
     * @var string
     */
    protected $template;

    /**
     * 构造函数
     * @param Config $config
     * @param Storage $storage
     */
    public function __construct(Config $config, Storage $storage){
        $this->config = $config;
        $this->engine = new Core();
        $this->data = new Data();
        $this->engine->setTemplateDir($config->dir('./template'));
        $this->engine->setCacheDir($storage->getCachePath('view'));
        $this->engine->setCompileDir($storage->getBuildPath('view'));
        $this->data->assign('storage', $storage);
    }

    /**
     * 设置路由组件
     * @param Collection $collection
     * @auto-injection
     */
    public function setRouter(Collection $collection){
        $this->data->assign('router', $collection);
    }

    /**
     * 获取视图内容
     * @return string
     */
    public function fetch():string{
        return $this->engine->get($this->template,$this->data);
    }

}